/*
 * @Descripttion: 功能
 * @Author: yoke
 * @Date: 2021-11-10 18:01:50
 */
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const fs = require("fs")
const path = require("path")
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	// 注册命令
	let commandOfGetFileState = vscode.commands.registerCommand('getFileState', uri => {
		let currentFileDir = uri?.path?.slice(1);
		vscode.window
			.showInputBox({
				placeHolder: `请输入目录名称`,
				prompt: `请输入目录名称`,
				value: "",
			})
			.then((pageDir) => {
				if (pageDir) {
					console.log(pageDir, currentFileDir, '................................')
					console.log(uri, '................................')
					fs.readdir(currentFileDir, (err, files) => {

						if (err) return vscode.window.showErrorMessage("文件创建失败！！")

						console.log(files, '...................................................')
						if (files.includes(pageDir)) {
							vscode.window.showErrorMessage('有相同的文件存在！！')
						} else {
							fs.mkdir(currentFileDir + `/${pageDir}`, function () {
								fs.writeFileSync(currentFileDir + `/${pageDir}/index.tsx`, `
import styles from "./index.less"

const ${pageDir}=()=>{

	return <div className={styles.${pageDir}}>
		${pageDir}
	</div>
}

export default ${pageDir};
								`)

								fs.writeFileSync(currentFileDir + `/${pageDir}/index.less`, `
@import "/src/global.less";
			
								`)
							})

							vscode.window.showInformationMessage(`文件创建成功`, { modal: true, detail: `在【${currentFileDir}】目录下` })
						}
					})
				} else {
					vscode.window.showErrorMessage('目录名称不能为空！')
				}
			})
	});

	// 将命令放入其上下文对象中，使其生效
	context.subscriptions.push(commandOfGetFileState);
}

// this method is called when your extension is deactivated
function deactivate() { }

module.exports = {
	activate,
	deactivate
}
